let getTreeCheckedId, uncheckedTree
var baseData
function createTree (datas, treeWrap, randomId, selectAll) {
  let html = ''
  html += '<div class="tree-container"><div class="tree-list-wrap" id="tree1' + randomId + '"></div><div class="tree-list-wrap" id="tree2' + randomId + '"></div><div class="tree-list-wrap" id="tree3' + randomId + '"></div><div class="tree-checked-wrap"><div class="checked-tree-wrap" id="tree4' + randomId + '"></div></div></div>'

  $(treeWrap).html(html)
  // 基础数据
  baseData = [
    {
      id: '6000001',
      title: '内部',
      arryName: 'innerList',
      type: 'index',
      // childrens: setArry(datas.innerList),
      childrens: formatCompanyData(datas.innerList, 'inner', '6000001', selectAll)
    },
    {
      id: '6000002',
      title: '外部',
      arryName: 'outerList',
      type: 'index',
      spread: true,
      children: [
        {
          id: '6000003',
          pid: '6000002',
          title: '好友公司',
          type: 'index',
          arryName: 'friendList',
          // childrens: setArry(datas.outerList.friendList),
          childrens: formatCompanyData(datas.outerList.friendList, 'friend', '6000003', selectAll)
        },
        {
          id: '6000004',
          pid: '6000002',
          title: '客商',
          type: 'index',
          arryName: 'customerList',
          // childrens: setArry(datas.outerList.customerList),
          childrens: formatCustomerData(datas.outerList.customerList, 'customer', '6000004', selectAll)
        },
        {
          id: '6000005',
          pid: '6000002',
          title: '供应商',
          type: 'index',
          arryName: 'supply',
          // childrens: setArry(datas.outerList.supplyList),
          childrens: formatCompanyData(datas.outerList.supplyList, 'supply', '6000005', selectAll)
        }
      ]
    }
  ]
  console.log(baseData)
  layui.use(function () {
    var tree = layui.tree;

    // if (selectAll && selectAll.length > 0) {
    //   const checketList = getCheckedList()
    //   createCheckedDom(checketList)
    // }

    renderTree()

    // 一级树
    function renderTree () {
      tree.render({
        elem: '#tree1' + randomId,  //绑定元素
        showCheckbox: true,
        onlyIconControl: true,
        data: baseData,
        id: 'demoId1',
        click: function (obj) {
          getResetData(baseData, obj.data.id, function (nextData) {
            renderTree2(nextData)  // 切换查看二级树
          })
        },
        oncheck: function (obj) {
          obj.checked ? obj.data.checked = true : obj.data.checked = false
          findChildData(obj.data, tree)
        }
      })
    }

    // 二级树
    function renderTree2 (itemData) {
      tree.render({
        elem: '#tree2' + randomId,  //绑定元素
        showCheckbox: true,
        onlyIconControl: true,
        data: itemData.childrens,
        id: 'demoId2',
        click: function (obj) {
          getResetData(baseData, obj.data.id, function (nextData) {
            renderTree3(nextData)
          })
        },
        oncheck: function (obj) {
          obj.checked ? obj.data.checked = true : obj.data.checked = false
          findChildData(obj.data, tree)
        }
      })
    }

    // 三级树
    function renderTree3 (itemData) {
      tree.render({
        elem: '#tree3' + randomId,  //绑定元素
        showCheckbox: true,
        data: formatDepartmentData(itemData.childrens),
        onlyIconControl: true,
        id: 'demoId3',
        click: function (obj) {

        },
        oncheck: function (obj) {

        }
      })
    }

  });

}





